﻿using GatewayServerService.CloudCenter;
using GatewayServerService.Data;
using GatewayServerService.Device;
using Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsCache;

namespace GatewayServerService.Mq
{
    public class ServiceMQPayEvent
    {     /// <summary>
          /// 处理事件
          /// </summary>
          /// <param name="json"></param>
        public bool payEvent(string json)
        {
            #region
            try
            {
                MQReqModel<object> httpReqModel = JsonConvert.DeserializeObject<MQReqModel<object>>(json);//, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Culture = new System.Globalization.CultureInfo("zh-CN") }


                //Console.WriteLine("############################################################");
                //Console.WriteLine("接收网关code:" + httpReqModel.gatewayCode);
                //Console.WriteLine("时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").ToString());
                //Console.WriteLine("接收网关MQ消息:" + json);
                //Console.WriteLine("############################################################");


                string objectjson = JsonConvert.SerializeObject(httpReqModel.data);

                switch (httpReqModel.respEvent)
                {
                    case MQEventEnum.Event_HandleCommand:
                        //网关事件处理
                        new ServiceCloudCallBack().callBackUpdate(httpReqModel);
                        break;
                    case MQEventEnum.Device_Gateway_IntervalLive:
                        //网关心跳
                        new ServiceCloudGateWay().callBackGateWay(JsonConvert.DeserializeObject<ModelGateway>(objectjson));
                        break;
                    case MQEventEnum.Device_Gateway_SoftWareUpgrade:
                        //网关升级
                        new ServiceCloudGateWay().callBack_softWareUpdateChangeStatus(int.Parse(httpReqModel.respCode), httpReqModel.respMsg, JsonConvert.DeserializeObject<UpgradeSoftWareModel>(objectjson));
                        break;
                    case MQEventEnum.Device_Command:
                        //网关固件指令
                        new ServiceCloudGateWay().callBack_doCommandStatus(int.Parse(httpReqModel.respCode), httpReqModel.respMsg, JsonConvert.DeserializeObject<MQSystemCommand>(objectjson));
                        break;
                    case MQEventEnum.API_DeviceControl:
                        //网关系统远程控制
                        new ServiceCloodAPIControl().deviceControl(httpReqModel);
                        break;
                    default:
                        Debug.WriteLine("RabbitMQ  " + httpReqModel.respEvent + " 跳过，未处理......");
                        new DBIotSystemLogError().addSystemLogError(httpReqModel.gatewayDeviceGuid, "网关 gatewayCode:" + httpReqModel.gatewayCode, "MQ消费,httpReqModel.respEvent:" + httpReqModel.respEvent + " 没有该事件的处理方法");
                        break;
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("payEvent： " + ex);
                return false;
            }
            #endregion
        }

    }
}
